/**
 * @param {character[][]} grid
 * @return {number}
 */
var numIslands = function (grid) {

    const m = grid.length;
    const n = grid[0].length;

    let flag = 0
    let res = 0
    const tool = (x, y) => {
        if (x < 0 || x >= m || y < 0 || y >= n) {
            return
        }
        if (grid[x][y] === '1') {
            grid[x][y] = '0';
            flag = 1
            tool(x-1,y)
            tool(x+1,y)
            tool(x,y-1)
            tool(x,y+1)

        }
    }
    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            tool(i, j)
            if (flag === 1) {
                res++
                flag = 0
            }
        }

    }
    return res
};

console.log(numIslands([["1", "1", "1", "1", "0"], ["1", "1", "0", "1", "0"], ["1", "1", "0", "0", "0"], ["0", "0", "0", "0", "0"]]))